import os
import sys
from time import sleep
import subprocess

def start_scripts():
    image_process = subprocess.Popen(["python", "image_data.py", sys.argv[1]], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    serial_process = subprocess.Popen(["python", "serial_data.py", sys.argv[1], sys.argv[1]], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    print("脚本开始运行")
    return [image_process, serial_process]

def stop_scripts(processes):
    for p in processes:
        p.terminate()
    print("脚本停止")

if __name__ == '__main__':
    # os.system("roscore &")
    # sleep(3) # 等待roscore启动完成
    if not os.path.exists(sys.argv[1]):
        print("路径有误！")
        sys.exit(1)
    try:
        processes = start_scripts()
        temp = 1
        while True:
            user_input = input("请输入传入参数：")
            try:
                temp = float(user_input)
            except ValueError:
                if user_input.lower() == 'v':
                    os.system(f"python serial_command.py -v {temp} &")
                elif user_input.lower() == 'p':
                    os.system(f"python serial_command.py -p {temp} &")
                else:
                    print("无效输入，请重新输入。")
            sleep(0.1)
    except KeyboardInterrupt:
        print("\r\nCTRL_C中断\r\n")
    finally:
        stop_scripts(processes)
